<?php
class Control_Recharge_fetchRechargeNum {

	function fetchRechargeNum() {
		$nowGame = $game 	= MooForm::request('game');
		$action 	= MooForm::request('action');
		$game = MooObj::get('Game')->transportGame($game);
		
		$nowChannel =  $channel 	= MooForm::request('channel');
		$platform 	= MooForm::request('platform');
		$type 		= MooForm::request('type');
		$dataType 		= MooForm::request('dataType');
		$beginDate 	= MooForm::request('startTime');
		$endDate 	= MooForm::request('endTime');
		
		$fetch_recharge_rank = MooConfig::get('main.rechargeUrl.fetch_recharge_rank');
		
		if ($action == 0) {
			$startDate 	= 	date('Y-m-d', time());
			$endDate 	= 	date('Y-m-d', time());
			MooView::set('startDate', $startDate);
			MooView::set('endDate', $endDate);
			MooView::render('fetchRechargeNum');
		} else {
			if($channel) {
				$channel = MooObj::get('Game')->transportChannel($game, $channel);
				$url = $fetch_recharge_rank."?game=".$game."&channel=".$channel."&beginDate=".$beginDate."&endDate=".$endDate;
			} else {
				$url = $fetch_recharge_rank."?game=".$game."&beginDate=".$beginDate."&endDate=".$endDate;
			}
			
			if($platform) {
				MooView::set('platform', $platform);
				$url = $url . "&platform=" . $platform;
			}
			
			MooObj::get('Control_Data')->setPlatformData($game);
			$rechargeSection = MooConfig::get('main.rechargeSection');
			
			$rechargeNumData= array();
			$rechargeNumData2= array();
			foreach ($rechargeSection as $key => $val) {
				$urls = $url . "&minPay=".$val['minPay']."&maxPay=".$val['maxPay']."&type=1";
				
				$rs = MooUtil::curl_send($urls, null, 5);
				$res = MooJson::decode($rs, true);
				$data['id'] 	=  $val['id']+1;
				$data['name'] 	=  $val['name'];
				$data['num'] 	=  $res['rangeCount'];
				$rechargeNumData[] =	$data;
				$rechargeNumData2[$key][0] =	$val['name'];
				$rechargeNumData2[$key][1] =	$res['rangeCount'];
			}
			
			$viewData = MooJson::encode($rechargeNumData2);
				
			// 格式化数据
			$viewData = str_replace(',"',',',$viewData);
			$viewData = str_replace('"]',']',$viewData);
			
			if($rechargeNumData && $type == "daochu") {
				$excelData = array();
				
				$sDate = date("Ymd", strtotime($beginDate));
				$eDate = date("Ymd", strtotime($endDate));
				
				if ($channel) {
					$name = $game. "_" . $channel . "_"  . $sDate . "_" . $eDate;
				} else {
					$name = $game. "_" .  $sDate . "_" . $eDate;
				}
				
				$excelRs = array(
					0 => array(
						0 => '',
						1 => $name,
					),
					1 => array(
						0 => '总充值区间',
						1 => '人数',
				));
				
				foreach ($rechargeNumData as $key=>$val) {
					$arr[] = $val['name'];
					$arr[] = $val['num'];
					$excelData[] = $arr;
					$arr = array();
				}
				
				$excelData = array_merge($excelRs, $excelData);
				$writeExcelArr[0]['name'] = $name;
				$writeExcelArr[0]['data'] = $excelData;
					
				$excelPath = ROOT_PATH . "/export/orders/" . $name . ".xls";
				$rs = MooObj::get('Control_ExcelConfMaker_Excel')->write($excelPath, $writeExcelArr);
				$this->downFile($excelPath, $name . ".xls");
				exit;
			}
			
			MooView::set('viewData', $viewData);
			MooView::set('data', $rechargeNumData);
			MooView::set('nowGame', $nowGame);
			MooView::set('channel', $nowChannel);
			MooView::set('startDate', $beginDate);
			MooView::set('endDate', $endDate);
			MooView::render('fetchRechargeNum');
		}
	}

	 // 下载文件
	 private function downFile($filepath, $fileName) {
		$fp			=	fopen($filepath,"r"); 
		$file_size	=	filesize($filepath); 
		//下载文件需要用到的头 
		header("Content-type: application/octet-stream"); 
		header("Accept-Ranges: bytes"); 
		header("Accept-Length:".$file_size); 
		header("Content-Disposition: attachment; filename=".$fileName); 
		$buffer		=	1024; 
		$file_count	=	0; 
		//向浏览器返回数据 
		while(!feof($fp) && $file_count < $file_size){ 
		$file_con	=	fread($fp,$buffer); 
		$file_count	+=	$buffer; 
		echo $file_con; 
		} 
		fclose($fp); 
	}
	
}